package com.qinsong.myapplication.controller.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.qinsong.myapplication.R;
import com.qinsong.myapplication.model.Model;
import com.qinsong.myapplication.utils.SaveLoginAndRegisterInfo;

public class RegisterActivity extends Activity {
    private EditText username;
    private EditText password;
    private EditText password2;
    private Button register;
    private Button relogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //初始化控件
        InitView();
        //初始化监听
        InitListener();
    }

    /**
     * 初始化控件
     */
    private void InitView() {
        username= findViewById(R.id.username);
        password=findViewById(R.id.password);
        password2= findViewById(R.id.password2);
        register=findViewById(R.id.register);
        relogin=findViewById(R.id.relogin);

    }

    /**
     * 监听页面按钮的点击事件
     */
    private void InitListener() {
        //注册按钮的点击事件处理
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
        //登录按钮点击事件
        relogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toLogin();
            }
        });
    }

    /**
     * 注册方法
     */
    private void register(){
        //获取输入的用户名和密码
        final String reusername = username.getText().toString().trim();
        final String repassword = password.getText().toString().trim();
        final String repassword2 = password2.getText().toString().trim();
        //校验输入的用用户名密码
        if (TextUtils.isEmpty(reusername) || TextUtils.isEmpty(repassword) || TextUtils.isEmpty(repassword2)){
            Toast.makeText(RegisterActivity.this,"用户名密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if (repassword!=repassword2 && !repassword.equals(repassword2)){
            Toast.makeText(RegisterActivity.this,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
            return;
        }
       ///去环信的服务器注册账号
        Model.getInstance().getGlobaThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    //注册信息
                    EMClient.getInstance().createAccount(reusername,repassword);
                    //更新页面
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //保存注册信息到data.txt
                            SaveLoginAndRegisterInfo.saveUserInfo(RegisterActivity.this,reusername,repassword);
                            Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                            //跳转页面
                            Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                } catch (final HyphenateException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(RegisterActivity.this,"注册失败"+e.toString(),Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });

    }

    /**
     * 返回登录界面
     */
    private void toLogin(){
        //使用意图对象
        Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
        startActivity(intent);
        finish();
    }

}
